/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.action;

import static javacommon.util.extjs.Struts2JsonHelper.outJson;
import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;
import static javacommon.util.extjs.Struts2JsonHelper.outJsonString;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.ResInfo;
import javacommon.util.org.TreeData;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.util.ObjectUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.pub.model.PubData;
import com.tech15.pub.service.PubDataManager;
import com.tech15.pub.vo.query.PubDataQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class PubDataAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String LIST_JSP= "/pub/pubData_list.jsp";
	protected static final String SUB_LIST_JSP= "/pub/pubData_list_sub.jsp";
	protected static final String EDIT_SUB_JSP= "/pub/pubData_edit_sub.jsp";
	protected static final String CREATE_JSP = "/pub/pubData_add.jsp";
	protected static final String EDIT_JSP = "/pub/pubData_edit.jsp";
	protected static final String MOVE_JSP = "/pub/pubData_move.jsp";
	protected static final String SHOW_JSP = "/pub/pubData_show.jsp";
	protected static final String MANAGER_JSP = "/pub/pubData_manager.jsp";
	protected static final String SELECT_JSP = "/pub/pubData_select.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pub/list.do";
	
	private PubDataManager pubDataManager;
	
	private PubData pubData;
	Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			pubData = new PubData();
		} else {
			pubData = (PubData)pubDataManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setPubDataManager(PubDataManager manager) {
		this.pubDataManager = manager;
	}	
	
	public Object getModel() {
		return pubData;
	}
	
	public void setId(Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	public String manager(){
		return MANAGER_JSP;
	}
	
	/*装载类别*/
	public String loadTree(){
		return SELECT_JSP;
	}
	
	public void tree(){
		List<TreeData> item = pubDataManager.findDataTree();
		TreeData root = new TreeData();
		root.setId("0");
		root.setItem(item);
		outJson(root);
	}
	
	/** 执行搜索 */
	public String list() {
		PubDataQuery query = newQuery(PubDataQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = pubDataManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	public String subList() {
		PubDataQuery query = newQuery(PubDataQuery.class,DEFAULT_SORT_COLUMNS);
		Page page = pubDataManager.findPage(query);
		savePage(page,query);
		return SUB_LIST_JSP;
	}
	
	public void getSubList(){
		Map map = new HashMap();
		List<PubData> list = pubDataManager.searchByPid(this.pubData.getPid());
		map.put("totalRecords", 0);
		map.put("root", list);
		//outJson(map);
		
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[] { "pubData", "pubDatas" });
		JSON json = JSONSerializer.toJSON(map, jsonConfig);  
		outJsonString(json.toString());
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		if(ObjectUtils.isNotEmpty(pubData.getPid())){
			PubData parent_pd = pubDataManager.getById(pubData.getPid());
			getRequest().setAttribute("parent_pd", parent_pd);
		}
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public void save() {
		Map<String, Object> resInfo = null;
		try{
			pubData.setIssys(0);
			pubDataManager.save(pubData);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,pubData.getId()+"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**进入更新页面*/
	public String edit() {
		if(ObjectUtils.isNotEmpty(pubData.getPid())){
			PubData parent_pd = pubDataManager.getById(pubData.getPid());
			getRequest().setAttribute("parent_pd", parent_pd);
		}
		return EDIT_JSP;
	}
	
	public String move() {
		if(ObjectUtils.isNotEmpty(pubData.getPid())){
			PubData parent_pd = pubDataManager.getById(pubData.getPid());
			getRequest().setAttribute("parent_pd", parent_pd);
		}
		return MOVE_JSP;
	}
	
	public String editSub() {
		return EDIT_SUB_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			pubDataManager.update(this.pubData);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	public void updateMove() {
		Map<String, Object> resInfo = null;
		try {
			pubDataManager.update(this.pubData);
			resInfo = ResInfo.getResInfo("", false, false, 2, OPERATE_SUCCESS, "");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try {
			if(this.pubData.getIssys() == 1){
				resInfo = ResInfo.getResInfo("",false, false, 2, "此数据不可删除","");
			}else{
				pubDataManager.removeById(id);
				resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
}
